package com.feather.common.db.impl;

import java.sql.Connection;
import java.sql.SQLException;

import com.feather.common.db.Transaction;

/**
 * @author flogyin
 */
public class NormalTransactionImpl implements Transaction {
	private ThreadLocal<Connection>	threadLocal;
	private Connection				connection;

	public NormalTransactionImpl(ThreadLocal<Connection> threadLocal, Connection conn) {
		this.threadLocal = threadLocal;
		this.connection = conn;
	}

	@Override
	public void commit() throws SQLException {
		try {
			this.connection.commit();
			this.connection.setAutoCommit(true);
		} finally {
			this.threadLocal.set(null);
			this.connection.close();
		}
	}

	@Override
	public void rollback() throws SQLException {
		try {
			this.connection.rollback();
			this.connection.setAutoCommit(true);
		} catch (SQLException ex) {
			throw ex;
		} finally {
			this.threadLocal.set(null);
			this.connection.close();
		}
	}
}
